package com.ebs.md.fxmonitor.server;

import java.util.Map;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Session;

import org.apache.log4j.Logger;

import com.ebs.commons.logger.MessageText;

import com.ebs.commons.comp.IParsedPayload;
import com.ebs.md.fxmonitor.topics.Keys;

/**
 * <p>Title: Netlink Project</p>
 *
 * <p>Description: Netlink Component</p>
 *
 * <p>Copyright: Copyright (c) 2007</p>
 *
 * <p>Company: ICAP</p> $Id: //depot/MarketData/rds/MAIN/src/main/java/com/ebs/md/rds/server/DestinationMap.java#1 $ not attributable $Revision: #1 $ 1.0
 */
public class DestinationMap {
    private Map<Keys.MessageTypes, MessageProducer> m_producers;
    private Session m_session;
    final static Logger s_logger = Logger.getLogger(DestinationMap.class);

    public DestinationMap() {
        super();
    }

    public void setProducers(Map<Keys.MessageTypes, MessageProducer> producers){
        m_producers = producers;
    }

    public void setSession(Session s){
        m_session = s;
    }

    public void sendMessage(Keys.MessageTypes messageType, IParsedPayload payload){
        try {
            Message mess = m_session.createObjectMessage(payload);
            sendMessage(messageType, mess);
        }
        catch (JMSException ex) {
            s_logger.error(new MessageText("NETLINK_0100004", payload), ex);
        }
    }

    public void sendMessage(Keys.MessageTypes messageType, Message message) throws JMSException {
        IParsedPayload payload = (IParsedPayload) ((ObjectMessage)message).getObject();
        payload.addField(Keys.Type, messageType.toString());
        m_producers.get(messageType).send(message);
    }
}
